package com.coocaa.ops.api.v1.policy.service;

import com.coocaa.ops.api.common.model.HeaderModel;
import com.coocaa.ops.api.v1.policy.cache.IPolicyCache;
import com.coocaa.ops.api.v1.policy.model.DeviceInfoModel;
import com.coocaa.ops.api.v1.policy.model.PolicyModel;
import com.coocaa.ops.api.v1.policy.util.PolicyQueryUtil;
import com.coocaa.ops.api.v1.policy.util.PolicyResultUtil;
import com.coocaa.ops.common.core.biz.policygroup.constant.PolicyGroupCategoryConstants;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupInfoEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tangminghong
 * @date 2020-06-23
 */
@Service
public class PolicyServiceImpl implements IPolicyService {

    @Autowired
    private IPolicyCache policyCache;

    @Override
    public PolicyModel getPolicyByHeader(HeaderModel header) {
        DeviceInfoModel deviceInfoModel = PolicyQueryUtil.convertToDeviceInfo(header);
        List<PolicyGroupInfoEntity> policyList = policyCache.getPolicyListByDeviceInfo(deviceInfoModel);
        if (CollectionUtils.isEmpty(policyList)) {
            return null;
        }
        for (PolicyGroupInfoEntity policy : policyList) {
            if (null != policy && isMatch(header, policy)) {
                return PolicyResultUtil.convertToPolicyModel(policy);
            }
        }
        return null;
    }

    @Override
    public PolicyModel getPolicyByHeader(HeaderModel header, String resourceType) {
        PolicyModel policyModel = getPolicyByHeader(header);
        return policyModel;
    }

    private boolean isMatch(HeaderModel header, PolicyGroupInfoEntity policy) {
        String startTcVersion = policy.getTcVersionStart() + "";
        String endTcVersion = policy.getTcVersionEnd() + "";
        String startAppVersion = policy.getHomepageVerStart();
        String endAppVersion = policy.getHomepageVerEnd();
        String size = policy.getScreenSize();

        boolean tcVersionMatch = header.getCTcVersion().compareToIgnoreCase(startTcVersion) >= 0 && header.getCTcVersion().compareToIgnoreCase(endTcVersion) <= 0;
        boolean appVersionMatch = header.getVAppVersion().compareToIgnoreCase(startAppVersion) >= 0 && header.getVAppVersion().compareToIgnoreCase(endAppVersion) <= 0;
        boolean sizeMatch = StringUtils.isEmpty(size) || size.equals(header.getCSize());
        if (PolicyGroupCategoryConstants.isTest(policy.getPolicyGroupCategory())) {
            boolean macMatch = true;
            if (StringUtils.isNotEmpty(header.getMac()) && StringUtils.isNotEmpty(policy.getMac())) {
                String[] macs = policy.getMac().split(",");
                macMatch = isMacMatch(macs, header);
            }
            return macMatch && tcVersionMatch && appVersionMatch && sizeMatch;
        }
        return tcVersionMatch && appVersionMatch && sizeMatch;
    }

    /**
     * 匹配mac
     *
     * @param macs
     * @param header
     * @return
     */
    private boolean isMacMatch(String[] macs, HeaderModel header) {
        if (null != macs && macs.length > 0) {
            for (String mac : macs) {
                if (header.getMac().equalsIgnoreCase(mac)) {
                    return true;
                }
            }
        }
        return false;
    }
}
